/******************************************************************
*
* Copyright 2010 Darshan M Sonde
*
* This file is part of ddots.
*
* ddots is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ddots is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ddots.  If not, see <http://www.gnu.org/licenses/>.
*
*********************************************************************/

#ifndef GAMESCENE_H
#define GAMESCENE_H

#include <QtGui>
#include "game.h"
#include "rohmbus.h"

#define PLAYER_ONE_TEXT "Player 1"
#define PLAYER_TWO_TEXT "Player 2"

#define BGCOLOR QColor(240,240,240)
#define PLAYER_ONE_COLOR  QColor(50,50,255)
#define PLAYER_TWO_COLOR  QColor(255,50,50)
#define LINECOLOR QColor(0,55,0)
#define POINTCOLOR QColor(10,100,10)
#define HIGHLIGHT_LINE_COLOR QColor(100,200,100)

#define PLAYER_ONE_GRADIENT_START QColor(100,155,100)
#define PLAYER_ONE_GRADIENT_END QColor(50,75,50)
#define PLAYER_TWO_GRADIENT_START QColor(120,120,155)
#define PLAYER_TWO_GRADIENT_END QColor(70,70,95)

class GameScene : public QGraphicsScene
{
    Q_OBJECT    

public:
    GameScene(QObject *parent=0);

    void setPlayerScore(Player player,int score);
    int playerScore(Player player);

public slots:
    void createGrid(int row,int col);
    void createBox(QPoint leftTop);

    void setTurn(Player player);
    Player turn();

    bool selectLine(QPoint start,QPoint end);

    void setWon(bool won);//sets win for currentTurn
    bool won();

    void clearGrid();
signals:
    void boxComplete(QPoint leftTop);
    void lineSelected(QPoint start,QPoint end);
    void showDialog();


protected:
    void keyPressEvent(QKeyEvent *event);


    inline void addElement(QGraphicsItem* item) { mBoxElements.append(item); }
    inline void addElement(QGraphicsEllipseItem *item) { mGridElements.append(item); }
    inline void addElement(Rohmbus *item) { mRohmbusElements.append(item); }
    void updateBoxCompletions(QPoint start,QPoint end);

    void updatePlayerStatus();
    void incrementCurrentPlayerScore();

protected slots:
    void gotMove(QPoint start,QPoint end);    

private:
    QGraphicsRectItem *mBase;
    QGraphicsTextItem *mPlayerOneStatus;
    QGraphicsTextItem *mPlayerTwoStatus;

    QList<QGraphicsEllipseItem*>  mGridElements;//required to clean
    QList<Rohmbus*> mRohmbusElements;
    QList<QGraphicsItem*> mBoxElements;//rect and text item

    QMap<QLine,bool> mBoxState;

    int mRow;//row
    int mCol;//col
    int mLineWidth;//line width
    int mLineHeight;//line height

    Player mCurrentTurn;
    int mPlayerOneScore;
    int mPlayerTwoScore;
    bool mWon;
};

inline bool operator <(const QLine &l1,const QLine &l2)
{ if(l1.x1()!=l2.x1())
    return l1.x1()<l2.x1();
  else if(l1.y1()!=l2.y1())
      return l1.y1()<l2.y1();
  else if(l1.x2()!=l2.x2())
      return l1.x2()<l2.x2();
  else
      return l1.y2()<l2.y2();
}
#endif // GAMESCENE_H
